{template 'web/'.$cfg['floder'].'/base_menu'}
<div class="top_tab"> 
  <a href="{php echo $this->createWebUrl('store',array('op'=>'display'))}" class="layui-btn {if $operation!='display'}layui-btn-primary{/if}">采购管理</a>
  <a href="{php echo $this->createWebUrl('store',array('op'=>'supplier'))}" class="layui-btn {if $operation!='supplier'}layui-btn-primary{/if}">供应商管理</a>
  <a href="{php echo $this->createWebUrl('store',array('op'=>'checkstock'))}" class="layui-btn {if $operation!='checkstock'}layui-btn-primary{/if}">商品盘点</a>
  <div class="secrchinput">
    <button><i class="fa fa-search"></i></button>
    <input type="text" id="txtTopupSeachStr" placeholder="输入">
  </div>
</div>
<style>
.panelbox-head i{ color:#009688;}
.panelbox-head a{ margin-right:10px}
</style>
{if $operation == 'supplier'}
<div class="panelbox">
  <div class="panelbox-head"> <A href="#" onclick="editBox(0)"><i class="fa fa-plus"></i>新增</A>  </div>
  <div class="panelbox-body">
    <div class="layui-form">
      <table class="layui-table">
        <colgroup>
          <col>
          <col>
          <col>
          <col>
          <col>
        </colgroup>
        <thead>
          <tr>
            <th style="width:60px"><input type="checkbox" name="" lay-skin="primary" lay-filter="allChoose"></th>
            <th>供应商</th>
            <th>采购金额</th>
            <th>联系人</th>
            <th>联系电话</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody>
          {loop $list $row}
          <tr rid="{$row['id']}">
            <td><input type="checkbox" name="" lay-skin="primary"></td>
            <td>{$row['title']}</td>
            <td></td>
            <td>{$row['realname']}</td>
            <td>{$row['mobile']}</td>
            <td class="text-right">
            <a href="javascript:editBox({$row['id']})" class="layui-btn layui-btn-normal layui-btn-small"><i class="layui-icon">&#xe642;</i></a> 
            <a href="javascript:deleteContent('{php echo $this->createWebUrl('store',array('op'=>'deletesupplier','id'=>$row['id']))}')" class="layui-btn layui-btn-danger layui-btn-small"><i class="layui-icon">&#xe640;</i></a></td>
          </tr>
          {/loop}
        </tbody>
      </table>
    </div>
  </div>
  <div class="panelbox-foot">  </div>
</div>
<div id="editcatebox" style="display:none">
<form id="editcateform" class="layui-form" action="" style="padding:15px">
<input type="hidden" name="id" />
<div class="layui-form-item">
  <div class="layui-form-item">
    <input type="text" name="title" lay-verify="required" placeholder="供应商名称" autocomplete="off" class="layui-input">
  </div>
  <div class="layui-form-item">
    <input type="text" name="realname" placeholder="联系人" autocomplete="off" class="layui-input">
  </div>
  <div class="layui-form-item">
    <input type="text" name="mobile" placeholder="联系电话" autocomplete="off" class="layui-input">
  </div>
  <div class="row">
    	<div class="col-md-12"><Div class="pull-right"> <button class="layui-btn" lay-submit="" lay-filter="btnsubmit">保存</button></Div></Div>
    </div>
</form>
</div>
<script language="javascript">
layui.use(['layer', 'laypage', 'element','form'], function(){
  var layer = layui.layer
  ,laypage = layui.laypage
  ,element = layui.element
  ,form = layui.form;
  
  form.on('submit(btnsubmit)', function(data){
      $.post("{php echo $this->createWebUrl('store',array('op'=>'postsupplier'))}",data.field,function(result){
		  if(!result.success){
			  layer.msg(result.msg,{icon:1,time:2000});
			  return;
		  }
		  location.reload();
	  },'json');
	  return false;
    return true;
  });
  
  
  form.on('checkbox(allChoose)', function(data){
    var child = $(data.elem).parents('table').find('tbody input[type="checkbox"]');
    child.each(function(index, item){
      item.checked = data.elem.checked;
    });
    form.render('checkbox');
  });
});
function deleteContent(url){
	layer.confirm('确认删除此内容？删除后将不可恢复', {}, function(){
		location.href=url;
	});
}

function editBox(id){
	var til= id==0 ? "新增":"修改";
	$("#editcateform")[0].reset();
	layer.open({
		type: 1,
		title:til,
		content: $("#editcatebox").show(),
		success:function(){
			if(id){
				var index2=layer.load(1, {shade: [0.5,'#fff']});
				$.post("{php echo $this->createWebUrl('store',array('op'=>'getsupplier'))}",{"id":id},function(data){
					layer.close(index2);
					if(!data.success){
						layer.msg(data.msg,{icon:1,time:2000});
						return;
					}
					$("#editcateform").find("input[name='id']").val(id);
					if(id>0){
						var items=data.item;
						$("#editcateform").find("input[name='title']").val(items.title);
						$("#editcateform").find("input[name='realname']").val(items.realname);
						$("#editcateform").find("input[name='mobile']").val(items.mobile);
					}
				},'json');
			}
		},
		end:function(){
			$("#editcatebox").hide();
		}
	});
}
</script>
{elseif $operation == 'display'}
<style>

</style>
<div class="panelbox">
<form action="" method="post" class="layui-form" id="listform" enctype="multipart/form-data">
  <div class="panelbox-head"> 
  	<A href="#" onclick="editBox(0)"><i class="fa fa-plus"></i> 新增</A>  
    <!--<A href="#"><i class="fa fa-log-out"></i> 导出</A> -->
  </div>
  <div class="panelbox-body">
    <div class="layui-form">
      <table class="layui-table">
        <thead>
          <tr>
            <th style="width:60px"><input type="checkbox" name="" lay-skin="primary" lay-filter="allChoose"></th>
            <th>单号</th>
            <th>供应商</th>
            <th>数量</th>
            <th>合计</th>
            <th>实付</th>
            <th>支付方式</th>
            <th>门店</th>
            <th>状态</th>
            <th>日期</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody>
          {php $paytpyeary=array("0"=>"微信","1"=>"支付宝","2"=>"现金","3"=>"银行卡","5"=>"其他")}
          {loop $list $row}
          <tr>
            <td><input type="checkbox" name="goodid[]" value="{$row['id']}" lay-skin="primary"></td>
            <td>{$row['ordersn']}</td>
            <td>{php echo $supplierary[$row['sid']]}</td>
            <td>{$row['num']}</td>
            <td>{$row['totalfee']}</td>
            <td>{$row['paidfee']}</td>
            <td>{php echo $paytpyeary[$row['paytype']]}</td>
            <td>{php echo $groupary[$row['groupid']]}</td>
            <td>{if $row['status']==1}<span class="label label-success">已入库</span>{elseif $row['status']==2}<span class="label label-danger">已退货</span>{else}<span class="label label-default">未处理</span>{/if}</td>
            <td><span class="label label-info">采</span> {php echo date("m/d H:i",$row['createtime'])}
            {if $row['instoretime']}<div style="margin-top:5px"></div><span class="label label-info">入</span> {php echo date("m/d H:i",$row['instoretime'])}</div>{/if}
            </td>
            <td class="text-right">
              <div class="btn-group">
                <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
                  操作 <span class="caret"></span>
                </button>
                <ul class="dropdown-menu" role="menu" style="right:0; left:auto">
                  <li><a href="javascript:" onclick="showview('{$row['ordersn']}')"><i class="fa fa-asterisk"></i> 详情</a></li>
                  <li><a href="javascript:" onclick="fun_print(6,'{$row['id']}')"><i class="fa fa-print"></i> 打印</a></li>
                  {if $row['status']==0}
                  <li class="divider"></li>
                  <li><a href="javascript:" onclick="fun_instore({$row['id']})"><i class="fa fa-asterisk"></i> 入库</a></li>
                  <li><a href="javascript:" onclick="editBox('{$row['ordersn']}')"><i class="fa fa-asterisk"></i> 编辑</a></li>
                  {/if}
                  
                  {if $row['status']==0}
                  <li class="divider"></li>
                  <li><a href="javascript:" onclick="deleteContent('{php echo $this->createWebUrl('store',array('op'=>'deletestore','id'=>$row['id']))}')"><i class="fa fa-trash"></i> 删除</a></li>
                  {elseif $row['status']==1}
                  <li class="divider"></li>
                  <li><a href="javascript:" onclick="salesreturn('{$row['ordersn']}')"><i class="fa fa-asterisk"></i> 退货</a></li>
                  {/if}
                </ul>
              </div>
            
            </td>
          </tr>
          {/loop}
        </tbody>
      </table>
    </div>
  </div>
  <div class="panelbox-foot">
  <div id="pageindex" class="pull-right"></div></div>
  </form>
</div>

<style>
.form_num1{ border-radius:4px; text-align:center; font-size:10px; width:60px; box-shadow:none;}
.form_num2{ border-radius:4px; text-align:center; font-size:10px; width:80px;box-shadow:none;}
#totalfee{color:#71cf3b; font-size:24px;}
.pricecss{color:#71cf3b; font-size:24px;}
#editgoodsform li{ padding:5px 0;}
#editgoodsform li.isselect a{color:#FF5722}
#editgoodsbox{ height:360px; overflow-y:scroll;}
li a,li a:active,li a:visited{ text-decoration:none;}

</style>
<div id="editcatebox" style="display:none;">
  <form id="editform" class="layui-form layui-form-pane" action="" style="padding:15px 15px 0 15px">
    <div class="row form-inline" style="padding:0 15px; margin-bottom:10px">
      <div class="input-group">
        <input type="text" class="form-control"  id="keyword" name="keyword" placeholder="条码或选择商品">
        <span class="input-group-btn">
        <button class="btn btn-default" onclick="showGoodsSelect()" type="button">选择</button>
        </span> 
      </div>
      <select id="sid" lay-ignore class="form-control" onChange="changeSid(this)">
        <option value="0" selected>选择供应商</option>
        
          {loop $supplierlist $row}
          
        <option value="{$row['id']}">{$row['title']}</option>
        
          {/loop}
        
      </select>
      <input type="submit" lay-submit="" lay-filter="searchbtn" id="searchbtn" style="display:none;" />
    </div>
  </form>
  <form id="editcateform" class="layui-form layui-form-pane" action="" style="padding:0 15px 15px 15px">
    <input type="hidden" name="sid" value="0" />
    <input type="hidden" name="ordersn" />
    <div style="height:250px; overflow-y:scroll;">
      <table class="layui-table">
        <thead>
          <tr>
            <th style="width:60px"></th>
            <th>条形码</th>
            <th>名称</th>
            <th>单位</th>
            <th>数量</th>
            <th>单价</th>
            <th>金额</th>
            <th>实收金额</th>
          </tr>
        </thead>
        <tbody>
        </tbody>
      </table>
    </div>
    <div class="row" style="border-top:1px solid #CCC; padding-top:10px; margin-bottom:15px">
      <div class="col-md-6 text-center">总金额:<span id="totalfee">￥0.00</span>
        <input type="hidden" name="totalfee">
      </div>
      <div class="col-md-4">实付
        <div class="layui-input-inline">
          <input type="text" name="paidfee" autocomplete="off" class="form-control">
        </div>
      </div>
    </div>
    <div class="row" style="border-bottom:1px solid #CCC; padding-bottom:10px; margin-bottom:15px">
      <div class="col-md-4">备注
        <div class="layui-input-inline">
          <input type="text" name="remark" placeholder="不多于50字" autocomplete="off" class="form-control">
        </div>
      </div>
      <div class="col-md-4">其他费用
        <div class="layui-input-inline">
          <input type="text" name="otherfee" autocomplete="off" class="form-control">
        </div>
      </div>
      <div class="col-md-4 form-inline">
        <select name="paytype" lay-verify="required" lay-ignore class="form-control">
          <option value="" selected>结账方式</option>
          <option value="0">微信</option>
          <option value="1">支付宝</option>
          <option value="2">现金</option>
          <option value="3">银行卡</option>
          <option value="5">其他</option>
        </select>
      </div>
    </div>
    <div class="row">
      <div class="col-md-12"><span id="ordersntxt"></span>
        <Div class="pull-right">
          <button class="layui-btn" lay-submit="" lay-filter="btnsubmit">保存</button>
        </Div>
      </Div>
    </div>
  </form>
</div>

<div id="editgoods" style="display:none;">
  <form id="editgoodsform" index class="layui-form layui-form-pane" action="" style="padding:15px">
    <div class="row">
    	<div class="col-md-2" >
        	<ul>
              {php $i=0;}
              {loop $goodcategory $row}
              <li pcate="{$row['id']}" onclick="getpcategood(this)" {if $i==0}class="isselect"{/if}><a href="#"><i class="fa fa-chevron-right"></i> {$row['title']}</a></li>
              {php $i++;}
              {/loop}
            </ul>
        </div>
        <div class="col-md-10" style="border-left:1px solid #CCC;">
        	<div id="editgoodsbox">
            	<table class="layui-table">
                  <thead>
                    <tr>
                      <th style="width:60px"><input type="checkbox" name="" lay-skin="primary" lay-filter="allChoose2"></th>
                      <th>编码</th>
                      <th>名称</th>
                      <th>参考进价</th>
                      <th>单位</th>
                    </tr>
                  </thead>
                  <tbody>
                    {loop $goodlist $row}
                      <tr gid="{$row['id']}">
                        <td><input type="checkbox" name="goodid[{$row['id']}]" value="{$row['id']}|{$row['goodsn']}|{$row['title']}|{$row['productprice']}|{$row['unitname']}" lay-skin="primary"></td>
                        <td>{$row['goodsn']}</td>
                        <td>{$row['title']}</td>
                        <td>{$row['productprice']}</td>
                        <td>{$row['unitname']}</td>
                      </tr>
                    {/loop}
                  </tbody>
                </table>
            </div>
        </div>
    </div>
    <div class="row text-right" style="padding:30px 15px 15px 15px">
    	<button class="layui-btn" type="button" onclick="layer.close($('#editgoodsform').attr('index'))">关闭</button>
        <button class="layui-btn" lay-submit="" lay-filter="btnselectgood">选择</button>
      </Div>
    </div>
  </form>
</div>

<div id="editsalereturn" style="display:none;">
  <form id="editsalereturnform" class="layui-form layui-form-pane" action="" style="padding:15px">
    <input type="hidden" name="ordersn" />
    <div style="height:340px; overflow-y:scroll; padding:10px 0">
      <table class="layui-table">
        <thead>
          <tr>
            <th style="width:60px"></th>
            <th>条形码</th>
            <th>名称</th>
            <th>单位</th>
            <th>剩余数量</th>
            <th>退货数量</th>
            <th>单价</th>
            <th>金额</th>
          </tr>
        </thead>
        <tbody>
        
        </tbody>
      </table>
    </div>
    <div class="row" style="border-top:1px solid #CCC; padding-top:10px; margin-bottom:15px">
      <div class="col-md-6 text-center">总金额:<span id="totalfee">￥0.00</span>
        <input type="hidden" name="totalfee">
      </div>
      <div class="col-md-4">实付
        <div class="layui-input-inline">
          <input type="text" name="paidfee" autocomplete="off" class="form-control">
        </div>
      </div>
    </div>
    <div class="row" style="border-bottom:1px solid #CCC; padding-bottom:10px; margin-bottom:15px">
      <div class="col-md-4">备注
        <div class="layui-input-inline">
          <input type="text" name="remark" placeholder="不多于50字" autocomplete="off" class="form-control">
        </div>
      </div>
      <div class="col-md-4">其他费用
        <div class="layui-input-inline">
          <input type="text" name="otherfee" autocomplete="off" class="form-control">
        </div>
      </div>
      <div class="col-md-4 form-inline">
        <select name="paytype" lay-verify="required" lay-ignore class="form-control">
          <option value="" selected>结账方式</option>
          <option value="0">微信</option>
          <option value="1">支付宝</option>
          <option value="2">现金</option>
          <option value="3">银行卡</option>
          <option value="5">其他</option>
        </select>
      </div>
    </div>
    <div class="row">
      <div class="col-md-12"><span id="ordersntxt"></span>
        <Div class="pull-right">
          <button class="layui-btn" lay-submit="" lay-filter="btnsalereturn">确认退货</button>
        </Div>
      </Div>
    </div>
  </form>
</div>
<div id="storeviewbox" style="display:none; min-height:500px"></div>
<audio id="sound_ok" src="{MODULE_URL}template/resources/img/dingdong.mp3"></audio>
<audio id="sound_warn" src="{MODULE_URL}template/resources/img/warn.mp3"></audio>
<iframe src="" id="print_iframe" name="print_iframe" style="width:1px; height:1px;"></iframe>
<script language="javascript">
function fun_print(ptype,orderid){
	layer.load(1,{shade: [0.1,'#fff'],time:5000});
	$("#print_iframe").removeAttr("src").attr("src","{$_W['siteroot']}app/index.php?i={$_W['uniacid']}&c=entry&ptype="+ptype+"&m=j_hmoney&do=print&orderid="+orderid);
}
layui.use(['layer', 'laypage', 'element','form'], function(){
	var layer = layui.layer
	,laypage = layui.laypage
	,element = layui.element
	,form = layui.form;
	form.on('submit(searchbtn)', function(data){
		if(data.field.keyword.length==0){
			layer.msg("请输入条码",{icon:1,time:1000});
			return false; 
		}
		$.post("{php echo $this->createWebUrl('store',array('op'=>'searchgoods'))}",data.field,function(result){
			$("#editform input[name='keyword']").val("");
			if(!result.success){
				layer.msg("无此商品",{icon:1,time:2000});
				$("#sound_warn")[0].play();
				return;
			}
			$("#sound_ok")[0].play();
			var goods=result.item;
			if($("#editcateform tr[gid='"+goods.id+"']").size()>0){
				var num=parseInt($("#editcateform tr[gid='"+goods.id+"']").find(".field1").val())+1;
				if(isNaN(num))num=1;
				$("#editcateform tr[gid='"+goods.id+"'] .field1").val(num);
				var price=parseFloat($("#editcateform tr[gid='"+goods.id+"'] .field2").val());
				var total=price*num;
				$("#editcateform tr[gid='"+goods.id+"'] .field3").val(total.toFixed(2));
				$("#editcateform tr[gid='"+goods.id+"'] .field4").val(total.toFixed(2));
			}else{
				var html='<tr gid="'+goods.id+'"><td><button type="button" onclick="delgoods('+goods.id+')" class="layui-btn layui-btn-danger layui-btn-mini"><i class="layui-icon">&#x1006;</i></button></td><td>'+goods.goodsn+'</td><td>'+goods.title+'</td><td>'+goods.unitname+'</td><td><input type="text" name="num['+goods.id+']" class="form_num1 field1" onchange="countfee('+goods.id+')" value="1"/></td><td><input type="text" name="price['+goods.id+']" class="form_num2 field2" value="'+goods.productprice+'" onchange="countfee('+goods.id+')"/></td><td><input type="text" name="total['+goods.id+']" readonly class="form_num2 field3" value="'+goods.productprice+'"/></td><td><input type="text" name="paid['+goods.id+']" class="form_num2 field4" onchange="fun_countFee()" value="'+goods.productprice+'"/></td></tr>';
				if($("#editcateform tbody tr").size()){
					$(html).insertBefore($("#editcateform tbody tr").eq(0));
				}else{
					$("#editcateform tbody").append(html);
				}
			}
			fun_countFee();
		},'json');
		return false;
	});
	form.on('submit(btnsubmit)', function(data){
		if(!parseInt(data.field.sid)){
			layer.msg("请选择供应商",{icon:1,time:2000});
			return false;
		}
		if(data.field.paytype==""){
			layer.msg("请选择结账方式",{icon:1,time:2000});
			return false;
		}
		$.post("{php echo $this->createWebUrl('store',array('op'=>'poststore'))}",data.field,function(result){
			if(!result.success){
				layer.msg(result.msg,{icon:1,time:2000});
				return;
			}
			location.reload();
		},'json');
		return false;
	});
	form.on('checkbox(allChoose)', function(data){
	  var child = $(data.elem).parents('table').find('tbody input[type="checkbox"]');
	  child.each(function(index, item){
		item.checked = data.elem.checked;
	  });
	  form.render('checkbox');
	});
	form.on('checkbox(allChoose2)', function(data){
	  var child = $(data.elem).parents('table').find('tbody input[type="checkbox"]');
	  child.each(function(index, item){
		item.checked = data.elem.checked;
	  });
	  form.render('checkbox');
	});
	form.on('submit(btnselectgood)', function(data){
		var items=JSON.stringify(data.field);
		if(items.length<3){
			layer.msg("请选择商品",{icon:1,time:2000});
			return false;
		}
		items=data.field;
		for(var i in items){
			var goods=items[i].split("|");
			if($("#editcateform tr[gid='"+goods[0]+"']").size()>0){
				var num=parseInt($("#editcateform tr[gid='"+goods[0]+"']").find(".field1").val())+1;
				if(isNaN(num))num=1;
				$("#editcateform tr[gid='"+goods[0]+"'] .field1").val(num);
				var price=parseFloat($("#editcateform tr[gid='"+goods[0]+"'] .field2").val());
				var total=price*num;
				$("#editcateform tr[gid='"+goods[0]+"'] .field3").val(total.toFixed(2));
				$("#editcateform tr[gid='"+goods[0]+"'] .field4").val(total.toFixed(2));
			}else{
				var html='<tr gid="'+goods[0]+'"><td><button type="button" onclick="delgoods('+goods[0]+')" class="layui-btn layui-btn-danger layui-btn-mini"><i class="layui-icon">&#x1006;</i></button></td><td>'+goods[1]+'</td><td>'+goods[2]+'</td><td>'+goods[4]+'</td><td><input type="text" name="num['+goods[0]+']" class="form_num1 field1" onchange="countfee('+goods[0]+')" value="1"/></td><td><input type="text" name="price['+goods[3]+']" class="form_num2 field2" value="'+goods[3]+'" onchange="countfee('+goods[0]+')"/></td><td><input type="text" name="total['+goods[0]+']" readonly class="form_num2 field3" value="'+goods[3]+'"/></td><td><input type="text" name="paid['+goods[0]+']" class="form_num2 field4" onchange="fun_countFee()" value="'+goods[3]+'"/></td></tr>';
				if($("#editcateform tbody tr").size()){
					$(html).insertBefore($("#editcateform tbody tr").eq(0));
				}else{
					$("#editcateform tbody").append(html);
				}
			}
		}
		fun_countFee();
		layer.close($('#editgoodsform').attr('index'));
		return false;
	});	
	
	form.on('submit(btnsalereturn)', function(data){
		if($("#editsalereturnform tbody tr").size()==0){
			layer.msg("没有退货商品",{icon:1,time:2000});
			return false;
		}
		var num=0;
		$("#editsalereturnform tbody .field1").each(function(index, element) {
            num+=isNaN(parseInt($(this).val())) ? 0 : parseInt($(this).val());
        });
		if(num==0){
			layer.msg("没有退货商品",{icon:1,time:2000});
			return false;
		}
		layer.confirm('确认退货？退货将直接扣减库存', {}, function(){
			$.post("{php echo $this->createWebUrl('store',array('op'=>'postsalereturn'))}",data.field,function(reslutstr){
				var result=eval("("+reslutstr+")");
				console.log();
				if(!result.success){
					layer.msg(result.msg,{icon:1,time:2000});
					return;
				}
				location.reload();
			});
		});
		return false;
	});
});
function countfee(id){
	if($("#editcateform tr[gid='"+id+"']").size()>0){
		var num=parseInt($("#editcateform tr[gid='"+id+"']").find("input[name^='num']").val());
		if(isNaN(num))num=1;
		$("#editcateform tr[gid='"+id+"'] .field1").val(num);
		var price=parseFloat($("#editcateform tr[gid='"+id+"'] .field2").val());
		var total=price*num;
		$("#editcateform tr[gid='"+id+"'] .field3").val(total.toFixed(2));
		$("#editcateform tr[gid='"+id+"'] .field4").val(total.toFixed(2));
	}
	fun_countFee();
}
function fun_countFee(){
	var totalnum=0;
	var totalfee=0;
	var paidfee=0;
	$("#editcateform tbody tr").each(function(index, element) {
        var num=parseInt($(this).find(".field1").val());
		if(isNaN(num)){
			num=1;
			$(this).find(".field1").val("1");
		}
		totalnum+=num;
		totalfee+=parseFloat($(this).find(".field3").val());
		paidfee+=parseFloat($(this).find(".field4").val());
    });
	$("#totalfee").text("￥"+totalfee.toFixed(2));
	$("#editcateform input[name='totalfee']").val(totalfee.toFixed(2));
	$("#editcateform input[name='paidfee']").val(paidfee.toFixed(2));
}
function getUrlParam(name){
	var reg = new RegExp("(^|&)"+name+"=([^&]*)(&|$)"); 
	var r =  window.location.search.substr(1).match(reg);
	var strValue = "";
	if (r!=null){
	 strValue= unescape(r[2]);
	}
	return strValue;
}
function changeSid(obj){
	$("#editcateform input[name='sid']").val($(obj).val());
}
function delgoods(num){
	$("#editcateform tr[gid='"+num+"']").remove();
	fun_countFee();
}
function deleteContent(url){
	layer.confirm('确认删除此内容？删除后将不可恢复', {}, function(){
		location.href=url;
	});
}

function editBox(id){
	var til= id==0 ? "新增采购单":"修改采购单";
	$("#editcateform")[0].reset();
	layer.open({
		type: 1,
		title:til+"-【{$shop['companyname']}】",
		area:'880px',
		skin: 'layui-layer-rim',
		content:$("#editcatebox").show(),
		success: function(layero, index){
			$("#editcateform tbody").empty();
			if(id){
				var index2=layer.load(1, {shade: [0.5,'#fff']});
				$.post("{php echo $this->createWebUrl('store',array('op'=>'getstoreno'))}",{"ordersn":id},function(data){
					layer.close(index2);
					if(id){
						var items=data.item;
						var goods=data.goodlist;
						$("#ordersntxt").text("单据编号 "+items.ordersn);
						$("#sid").val(items.sid);
						$("#editcatebox input[name='sid']").val(items.sid);
						$("#editcatebox select[name='paytype']").val(items.paytype);
						$("#editcatebox #totalfee").text("￥"+items.totalfee);
						$("#editcatebox input[name='totalfee']").val(items.totalfee);
						$("#editcatebox input[name='paidfee']").val(items.paidfee);
						$("#editcatebox input[name='otherfee']").val(items.otherfee);
						$("#editcatebox input[name='remark']").val(items.remark);
						//获取订单进行初始化
						var temp="";
						for(var i in goods){
							temp+='<tr gid="'+goods[i].goodid+'"><td><button type="button" onclick="delgoods('+goods[i].goodid+')" class="layui-btn layui-btn-danger layui-btn-mini"><i class="layui-icon">&#x1006;</i></button></td><td>'+goods[i].goodsn+'</td><td>'+goods[i].title+'</td><td>'+goods[i].unitname+'</td><td><input type="text" name="num['+goods[i].goodid+']" class="form_num1 field1" onchange="countfee('+goods[i].goodid+')" value="'+goods[i].num+'"/></td><td><input type="text" name="price['+goods[i].goodid+']" class="form_num2 field2" value="'+goods[i].price+'" onchange="countfee('+goods[i].goodid+')"/></td><td><input type="text" name="total['+goods[i].goodid+']" readonly class="form_num2 field3" value="'+goods[i].totalfee+'"/></td><td><input type="text" name="paid['+goods[i].goodid+']" class="form_num2 field4" onchange="fun_countFee()" value="'+goods[i].paidfee+'"/></td></tr>';
						}
						$("#editcateform tbody").append(temp);
					}
				},'json');
			}else{
				$("#ordersntxt").text("");
			}
		},
		end:function(){
			$("#editcatebox").hide();
			$("#editcatebox #totalfee").text("￥0.00");
		}
	});
}

function fun_instore(id){
	layer.confirm('确认入库？入库后，商品数量将自动增加。', {}, function(){
		var index2=layer.load(1, {shade: [0.5,'#fff']});
		$.post("{php echo $this->createWebUrl('store',array('op'=>'instore'))}",{"id":id},function(result){
			layer.close(index2);
			if(!result.success){
				layer.msg(result.msg,{icon:1,time:2000});
				return;
			}
			location.reload();
		},'json');
	});
}
function showGoodsSelect(){
	layer.open({
		type: 1,
		title:"选择商品",
		area:'680px',
		skin: 'layui-layer-rim',
		content:$("#editgoods").show(),
		success: function(layero, index){
			$("#editgoodsform").attr("index",index);
			$("#editgoodsform")[0].reset();
		},
		end:function(){
			$("#editgoods").hide();
		},
	})
}
function getpcategood(obj){
	if($(obj).hasClass("isselect"))return;
	$(obj).addClass("isselect").siblings().removeClass("isselect");
	var pcate=$(obj).attr("pcate");
	var index2=layer.load(1, {shade: [0.1,'#fff']});
	$.post("{php echo $this->createWebUrl('store',array('op'=>'getpcategood'))}",{"pcate":pcate},function(result){
		layer.close(index2);
		$("#editgoodsform tbody").empty();
		if(result.success){
			var goods=result.list;
			var temp="";
			for(var i in goods){
				temp+='<tr gid="'+goods[i].id+'"><td><input type="checkbox" name="goodid['+goods[i].id+']" value="'+goods[i].id+'|'+goods[i].goodsn+'|'+goods[i].title+'|'+goods[i].productprice+'|'+goods[i].unitname+'" lay-skin="primary"></td><td>'+goods[i].goodsn+'</td><td>'+goods[i].title+'</td><td>'+goods[i].productprice+'</td><td>'+goods[i].unitname+'</td></tr>';
			}
			$("#editgoodsform tbody").html(temp);
			layui.use(['form'], function(){
				var form = layui.form;
				form.render('checkbox');
			});
		}
	},'json');
}

function salesreturn(id){
	$("#editsalereturnform")[0].reset();
	layer.open({
		type: 1,
		title:"退货",
		area:'880px',
		skin: 'layui-layer-rim',
		content:$("#editsalereturn").show(),
		success: function(layero, index){
			$("#editsalereturnform tbody").empty();
			var index2=layer.load(1, {shade: [0.5,'#fff']});
			$("#editsalereturnform #ordersntxt").text();
			$.post("{php echo $this->createWebUrl('store',array('op'=>'getstoreno'))}",{"ordersn":id},function(data){
				layer.close(index2);
				if(id){
					var items=data.item;
					var goods=data.goodlist;
					$("#editsalereturnform input[name='ordersn']").val(id);
					$("#editsalereturnform #totalfee").text("￥"+items.totalfee);
					$("#editsalereturnform input[name='totalfee']").val(items.totalfee);
					$("#editsalereturnform input[name='paidfee']").val(items.paidfee);
					$("#editsalereturnform input[name='otherfee']").val(items.otherfee);
					var temp="";
					for(var i in goods){
						temp+='<tr gid="'+goods[i].goodid+'"><td><button type="button" onclick="delgoods2('+goods[i].goodid+')" class="layui-btn layui-btn-danger layui-btn-mini"><i class="layui-icon">&#x1006;</i></button></td><td>'+goods[i].goodsn+'</td><td>'+goods[i].title+'</td><td>'+goods[i].unitname+'</td><td class="maxnum">'+goods[i].num+'</td><td><input type="text" name="num['+goods[i].goodid+']" class="form_num1 field1" onchange="countfee2('+goods[i].goodid+')" value="'+goods[i].num+'"/></td><td><input type="text" name="price['+goods[i].goodid+']" class="form_num2 field2" readonly value="'+goods[i].price+'" /></td><td><input type="text" name="paid['+goods[i].goodid+']" class="form_num2 field4" onchange="fun_countFee2()" value="'+goods[i].paidfee+'"/></td></tr>';
					}
					$("#editsalereturnform tbody").append(temp);
				}
			},'json');
		},
		end:function(){
			$("#editsalereturn").hide();
			$("#editsalereturnform #totalfee").text("￥0.00");
		}
	});
}
function countfee2(id){
	if($("#editsalereturnform tr[gid='"+id+"']").size()>0){
		var num=parseInt($("#editsalereturnform tr[gid='"+id+"']").find("input[name^='num']").val());
		if(isNaN(num))num=1;
		if(num<0)num=0;
		var maxnum=parseInt($("#editsalereturnform tr[gid='"+id+"']").find(".maxnum").text());
		if(num>maxnum)num=maxnum;
		$("#editsalereturnform tr[gid='"+id+"'] .field1").val(num);
		var price=parseFloat($("#editsalereturnform tr[gid='"+id+"'] .field2").val());
		var total=price*num;
		$("#editsalereturnform tr[gid='"+id+"'] .field4").val(total.toFixed(2));
	}
	fun_countFee2();
}
function fun_countFee2(){
	var totalnum=0;
	var totalfee=0;
	var paidfee=0;
	$("#editsalereturnform tbody tr").each(function(index, element) {
        var num=parseInt($(this).find(".field1").val());
		if(isNaN(num)){
			num=1;
			$(this).find(".field1").val("1");
		}
		totalnum+=num;
		paidfee+=parseFloat($(this).find(".field4").val());
    });
	$("#editsalereturnform #totalfee").text("￥"+paidfee.toFixed(2));
	$("#editsalereturnform input[name='totalfee']").val(paidfee.toFixed(2));
	$("#editsalereturnform input[name='paidfee']").val(paidfee.toFixed(2));
}
function delgoods2(num){
	$("#editsalereturnform tr[gid='"+num+"']").remove();
	fun_countFee2();
}
function showview(ordersn){
	layer.open({
		type: 1,
		title:"查看采购单",
		area:'880px',
		skin: 'layui-layer-rim',
		btn:['关闭'],
		btn1:function(index, layero){
			layer.close(index);
		},
		content:$("#storeviewbox").show(),
		success:function(){
			$("#storeviewbox").load("{php echo $this->createWebUrl('store',array('op'=>'storeview'))}&ordersn="+ordersn);
		},
		end:function(){
			$("#storeviewbox").show().empty();
		}
	});
}
</script>
{elseif $operation == 'checkstock'}
<style>
.anum{ color:#F30; font-weight:bold;}
.dnum{ color:#036; font-weight:bold;}
.totalnum{ font-size:16px; color:#F30; font-weight:bold;}
.totalnum2{ font-size:16px; color:#C30; font-weight:bold;}
tr.red td{ background:#F7B824;color:#FFF}
</style>
<div class="panelbox">
  
  <div class="panelbox-head"> 
    <A href="#" onClick="output()"><i class="fa fa-log-out"></i> 导出盘点数据</A> 
  </div>
  <form action="" method="post" class="layui-form" id="listform" enctype="multipart/form-data">
  <div class="panelbox-head form-inline"> 
    <input type="text" class="form-control"  id="keyword" name="keyword" placeholder="输入条码">
    <button type="button" class="btn btn-default">商品种类：<span class="totalnum" id="totals">0</span></button>
    <button type="button" class="btn btn-default">商品总数：<span class="totalnum2" id="totaln">0</span></button>
    <button type="button" class="btn btn-info" onclick="$('#btnstore').click()">核对商品数量</button>
  </div>
  <input type="submit" lay-submit="" lay-filter="searchbtn" id="searchbtn" style="display:none;" />
  </form>
  <form action="" method="post" class="layui-form" id="listform" enctype="multipart/form-data">
  <div class="panelbox-body">
    <div class="layui-form">
      <table class="layui-table" lay-even="">
        <thead>
          <tr>
            <th>名称</th>
            <th>单位</th>
            <th>数量</th>
            <th>核对</th>
            <th style="text-align:right;">操作</th>
          </tr>
        </thead>
        <tbody id="goodlistbox">
          
        </tbody>
      </table>
    </div>
  </div>
  <button style="display:none" class="layui-btn" id="btnstore" lay-submit="" lay-filter="btnstore">保存</button>
  </form>
</div>
<audio id="sound_ok" src="{MODULE_URL}template/resources/img/dingdong.mp3"></audio>
<audio id="sound_warn" src="{MODULE_URL}template/resources/img/warn.mp3"></audio>
<script language="javascript">
layui.use(['layer', 'laypage', 'element','form'], function(){
	var layer = layui.layer
	,laypage = layui.laypage
	,element = layui.element
	,form = layui.form;
	form.on('submit(searchbtn)', function(data){
		if(data.field.keyword.length==0){
			layer.msg("请输入条码",{icon:1,time:1000});
			return false; 
		}
		$.post("{php echo $this->createWebUrl('store',array('op'=>'searchgoods'))}",data.field,function(result){
			$("input[name='keyword']").val("");
			if(!result.success){
				layer.msg("无此商品",{icon:1,time:1000});
				$("#sound_warn")[0].play();
				return false;
			}
			$("#sound_ok")[0].play();
			var goods=result.item;
			if($("#goodlistbox tr[gid='"+goods.id+"']").size()>0){
				var num=parseInt($("#goodlistbox tr[gid='"+goods.id+"']").find(".num").val())+1;
				$("#goodlistbox tr[gid='"+goods.id+"'] .num").val(num);
				$("#goodlistbox tr[gid='"+goods.id+"'] .anum").text(num);
			}else{
				var html='<tr gid="'+goods.id+'"><td>'+goods.title+'<input type="hidden" name="num['+goods.id+']" class="num" value="1"/></td><td>'+goods.unitname+'</td><td class="anum">1</td><td class="dnum"></td><td class="text-right"><button type="button" onclick="reduce('+goods.id+')" class="btn btn-info btn-sm"><i class="fa fa-minus"></i></button></td></tr>';
				if($("#goodlistbox tr").size()){
					$(html).insertBefore($("#goodlistbox tr").eq(0));
				}else{
					$("#goodlistbox").append(html);
				}
			}
			fun_countnum();
		},'json');
		return false;
	});
	form.on('submit(btnstore)', function(data){
		if($("#goodlistbox tr").size()==0){
			layer.msg("请先录入商品",{icon:1,time:1000});
			return false; 
		}
		var index2=layer.load(1, {shade: [0.5,'#fff']});
		$.post("{php echo $this->createWebUrl('store',array('op'=>'getgoodstore'))}",data.field,function(resultstr){
			layer.close(index2);
			var result=eval("("+resultstr+")");
			if(!result.success){
				layer.msg(result.msg,{icon:1,time:1000});
				return false; 
			}
			var list=result.list;
			for(var i in list){
				if($("#goodlistbox tr[gid='"+list[i].id+"']").size()){
					$("#goodlistbox tr[gid='"+list[i].id+"']").find(".dnum").text(list[i].store);
					var num=parseInt($("#goodlistbox tr[gid='"+list[i].id+"']").find(".anum").text());
					if(num!=parseInt(list[i].store)){
						$("#goodlistbox tr[gid='"+list[i].id+"']").addClass("red");
					}
				}
			}
		});
		return false;
	});
});
function fun_countnum(){
	var totals=$("#goodlistbox tr").size();
	var totaln=0;
	$("#goodlistbox .num").each(function(index, element) {
        var num=parseInt($(this).val());
		totaln+=num;
    });
	$("#totals").text(totals);
	$("#totaln").text(totaln);
}
function reduce(id){
	var num=parseInt($("#goodlistbox tr[gid='"+id+"']").find(".num").val());
	if(num<=1){
		$("#goodlistbox tr[gid='"+id+"']").remove();
		return;
	}
	num--;
	$("#goodlistbox tr[gid='"+id+"'] .num").val(num);
	$("#goodlistbox tr[gid='"+id+"'] .anum").text(num);
}
</script>
{/if}